<template>
  <div class="content-page">
    <div class="header flex justify-between items-center mb-8">
      <h1 class="text-2xl font-semibold">特殊人群页面</h1>
      <el-button type="primary" @click="showDialog = true">上传认证</el-button>
    </div>

    <div class="certificate-gallery">
      <div class="certificate-container" v-for="(certificate, index) in certificates" :key="index">
        <h3>{{ certificate.name }}</h3>
        <img :src="certificate.image" :alt="`Certificate Image ${index + 1}`" style="width: 200px;height:200px">
        <div class="certificate-info">
          <p><strong>特殊类型:</strong> {{ certificate.type }}</p>
          <p><strong>严重程度:</strong> {{ certificate.level }}</p>
          <p><strong>认证时间:</strong> {{ certificate.startTime }}</p>
        </div>
        <button class="delete-button" @click="confirmRemoveCertificate(index)">删除</button>
      </div>
    </div>

    <!-- 弹窗 -->
    <div v-if="showDialog" class="dialog-overlay fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
      <div class="dialog-content bg-white rounded-tl-lg rounded-bl-lg w-[80vw] max-w-[800px] p-8">
        <div class="dialog-header mb-6">
          <h2 class="text-xl font-medium">提交认证信息</h2>
        </div>
        
        <div class="dialog-body">
          <form @submit.prevent="handleSubmit">
            <!-- 表单内容保持不变 -->
            <div class="form-item mb-4">
              <label class="required-label block mb-2">病症:</label>
              <el-input 
                v-model="formData.name"
                placeholder="请输入病症"
                class="w-full text-sm"
              />
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">特殊类型:</label>
              <el-select 
                v-model="formData.type"
                placeholder="请选择特殊类别"
                class="w-full text-sm"
              >
                <el-option label="科技创新" value="tech" />
                <el-option label="学科竞赛" value="subject" />
                <el-option label="创业竞赛" value="startup" />
              </el-select>
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">严重程度:</label>
              <el-select
                v-model="formData.level" 
                placeholder="请选择获奖级别"
                class="w-full text-sm"
              >
                <el-option label="国家级" value="national" />
                <el-option label="省级" value="provincial" />
                <el-option label="市级" value="city" />
              </el-select>
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">认证时间:</label>
              <div class="flex items-center gap-4">
                <el-date-picker
                  v-model="formData.startTime"
                  type="date"
                  placeholder="选择开始时间"
                  class="flex-1 text-sm"
                />
                <!-- <span class="text-gray-400">-</span>
                <el-date-picker
                  v-model="formData.endTime"
                  type="date"
                  placeholder="选择结束时间"
                  class="flex-1 text-sm"
                /> -->
              </div>
            </div>

            <div class="form-item mb-6">
              <label class="required-label block mb-2">图片证明材料:</label>
              <div 
                class="upload-area border-2 border-dashed border-gray-200 rounded-lg p-6 text-center cursor-pointer hover:border-blue-400 transition-colors"
                @click="triggerUpload"
              >
                <el-icon class="text-gray-400 text-2xl mb-2"><Plus /></el-icon>
                <input 
                  ref="fileInput"
                  type="file"
                  class="hidden"
                  accept="image/*"
                  @change="handleFileChange"
                >
                <div class="text-gray-500 text-sm">点击或拖拽上传图片</div>
                <div class="text-gray-400 text-xs mt-1">图片尺寸规则:A4纸为准,大小不大于1M</div>
              </div>
            </div>

            <div class="dialog-footer flex justify-end gap-4">
              <el-button @click="handleCancel" class="!rounded-button whitespace-nowrap">返回</el-button>
              <el-button 
                type="primary" 
                @click="handleSubmit"
                :disabled="!isFormValid"
                class="!rounded-button whitespace-nowrap"
              >
                提交
              </el-button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const showDialog = ref(false);
const fileInput = ref(null);

// interface FormData {
//   name: string;
//   type: string;
//   level: string;
//   startTime: string;
//   endTime: string;
//   organizer: string;
//   description: string;
//   file: File | null;
// }

const formData = ref<FormData>({
  name: '',
  type: '',
  level: '',
  startTime: '',
  endTime: '',
  organizer: '',
  description: '',
  file: null
});

const isFormValid = computed(() => {
  return formData.value.name && 
         formData.value.type && 
         formData.value.level && 
         formData.value.startTime && 
         formData.value.endTime &&
         formData.value.organizer &&
         formData.value.description;
});

const triggerUpload = () => {
  fileInput.value?.click();
};

const handleFileChange = (event) => {
  const target = event.target ;
  if (target.files && target.files[0]) {
    const file = target.files[0];
    if (file.size > 1024 * 1024) {
      ElMessage.error('文件大小不能超过1M');
      return;
    }
    formData.value.file = file;
  }
};

const handleSubmit = () => {
  if (!isFormValid.value) {
    ElMessage.warning('请填写完整所有必填项');
    return;
  }
  ElMessage.success('提交成功');
  showDialog.value = false; // 提交成功后关闭弹窗
};

const handleCancel = () => {
  showDialog.value = false;
  ElMessage.info('已取消');
};

const certificates = ref([
  { name: "国际数学建模竞赛 (MCM/ICM)", type: "数学建模", level: "国际", startTime: "2024-02-01", endTime: "2024-02-05", organizer: "美国数学协会", description: "Meritorious Winner (M奖)", image: 'https://ai-public.mastergo.com/ai/img_res/139ab9e3299a30b1bcaf87c75ea8d69e.jpg' },
  { name: "ACM-ICPC 亚洲区域总决赛", type: "程序设计", level: "国际", startTime: "2023-12-01", endTime: "2023-12-03", organizer: "国际计算机学会", description: "银牌", image: "https://ai-public.mastergo.com/ai/img_res/8a2d123aef8db4d2e017a1c174edd401.jpg" },
  { name: "全国大学生数学建模竞赛", type: "数学建模", level: "全国", startTime: "2023-10-10", endTime: "2023-10-13", organizer: "中国工业与应用数学学会", description: "一等奖", image: "https://ai-public.mastergo.com/ai/img_res/75979d8b6a3d45c38eb8e6124d3e438e.jpg" },
  { name: "全国电子设计竞赛", type: "电子设计", level: "全国", startTime: "2023-08-20", endTime: "2023-08-25", organizer: "教育部", description: "二等奖", image: "https://ai-public.mastergo.com/ai/img_res/c8e123567c3b4e2a8c7a1c2a4f963ae9.jpg" },
  { name: "全球AI挑战赛", type: "人工智能", level: "国际", startTime: "2023-11-15", endTime: "2023-11-20", organizer: "全球AI协会", description: "冠军", image: "https://ai-public.mastergo.com/ai/img_res/dfcb2b123456789a1bcdfge12345.jpg" },
  { name: "黑客马拉松", type: "编程", level: "国际", startTime: "2024-01-10", endTime: "2024-01-12", organizer: "Hackathon社区", description: "季军", image: "https://ai-public.mastergo.com/ai/img_res/789cdef4567b345c12abcdf56789.jpg" },
]);

function confirmRemoveCertificate(index) {
  ElMessageBox.confirm(
    '您确定要删除这个认证记录吗？此操作不可逆！',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    removeCertificate(index);
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    });
  });
}

function removeCertificate(index) {
  certificates.value.splice(index, 1);
}
</script>


<style scoped>
.required-label::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}

.dialog-overlay {
  z-index: 1000;
}

.dialog-content {
  width: 90vw;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  background: #fff;
  border-radius: 1rem 0 0 1rem; /* 仅左上角和左下角是圆角 */
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 自定义滚动条样式 */
.dialog-content::-webkit-scrollbar {
  width: 8px; /* 滚动条宽度 */
}

.dialog-content::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道背景 */
  border-radius: 1rem; /* 确保轨道有圆角 */
}

.dialog-content::-webkit-scrollbar-thumb {
  background: #888; /* 滚动条颜色 */
  border-radius: 1rem; /* 确保滚动条有圆角 */
}

.dialog-content::-webkit-scrollbar-thumb:hover {
  background: #555; /* 悬停时滚动条颜色 */
}

.certificate-gallery {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* 每行显示两个证书 */
  gap: 2rem; /* 证书之间的间距 */
  justify-items: center; /* 证书居中对齐 */
}

.certificate-container {
  position: relative; /* 使删除按钮能够定位在证书盒子内 */
  border-radius: 1rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 1rem;
  text-align: center;
  width: 400px;
  max-width: 400px; /* 设置证书盒子的最大宽度 */
}

h3 {
  font-size: 1.25rem;
  color: #333;
}

img {
  display: block;
  margin: 0.5rem auto; /* 图片居中 */
  border-radius: 0.5rem;
}

.certificate-info {
  text-align: left; /* 信息左对齐 */
  margin-top: 1rem;
}

p {
  font-size: 1rem;
  color: #00796b;
  margin: 0.5rem 0;
}

.delete-button {
  position: absolute; /* 定位在证书盒子的右下角 */
  bottom: 1rem;
  right: 1rem;
  background-color: red;
  color: white;
  border: none;
  padding: 0.3rem 0.5rem; /* 按钮尺寸更小 */
  border-radius: 0.5rem;
  cursor: pointer;
}

.delete-button:hover {
  background-color: darkred; /* 鼠标悬停时改变按钮颜色 */
}
</style>
